﻿namespace DeltaGroup.WheelOfJeopardy.UI.Screens
{
    using System;
    using System.Windows.Controls;
    using System.Windows.Media.Animation;

    /// <summary>
    /// Interaction logic for ScreenTransition.xaml
    /// </summary>
    public partial class ScreenTransition : UserControl
    {
        #region Fields

        private static readonly string WIPE_IN_RESOURCEKEY = "wipeInStoryboard";
        private static readonly string WIPE_OUT_RESOURCEKEY = "wipeOutStoryboard";

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ScreenTransition"/> class.
        /// </summary>
        public ScreenTransition()
        {
            InitializeComponent();
        }

        #endregion Constructors

        #region Events

        /// <summary>
        /// Occurs when [wipe in completed].
        /// </summary>
        public event EventHandler WipeInCompleted;

        /// <summary>
        /// Occurs when [wipe out completed].
        /// </summary>
        public event EventHandler WipeOutCompleted;

        #endregion Events

        #region Methods

        /// <summary>
        /// Wipes the in.
        /// </summary>
        public void WipeIn()
        {
            Storyboard wipeIn = GetStoryboard(WIPE_IN_RESOURCEKEY);
            wipeIn.Begin();
        }

        /// <summary>
        /// Wipes the out.
        /// </summary>
        public void WipeOut()
        {
            Storyboard wipeOut = GetStoryboard(WIPE_OUT_RESOURCEKEY);
            wipeOut.Begin();
        }

        /// <summary>
        /// Gets the storyboard.
        /// </summary>
        /// <param name="resourceKey">The resource key.</param>
        /// <returns></returns>
        private Storyboard GetStoryboard(string resourceKey)
        {
            Storyboard storyboard = this.Resources[resourceKey] as Storyboard;
            if (storyboard == null)
            {
                throw new MissingFieldException(string.Format("Storyboard [{0}] not found in Wheel resources.",resourceKey));
            }

            return storyboard;
        }

        /// <summary>
        /// Raises the wipe in completed event.
        /// </summary>
        private void RaiseWipeInCompletedEvent()
        {
            if (WipeInCompleted != null) {
                WipeInCompleted(this, null);
            }
        }

        /// <summary>
        /// Raises the wipe out completed event.
        /// </summary>
        private void RaiseWipeOutCompletedEvent()
        {
            if (WipeOutCompleted != null)
            {
                WipeOutCompleted(this, null);
            }
        }

        /// <summary>
        /// Handles the Completed event of the wipeInStoryboard control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void wipeInStoryboard_Completed(object sender, EventArgs e)
        {
            RaiseWipeInCompletedEvent();
            WipeOut();
        }

        /// <summary>
        /// Handles the Completed event of the wipeOutStoryboard control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void wipeOutStoryboard_Completed(object sender, EventArgs e)
        {
            RaiseWipeOutCompletedEvent();
        }

        #endregion Methods
    }
}